<template>
  <el-form ref="elForm" :model="formData" size="medium" label-width="120px">
    <el-form-item label="部门名称">
      <el-input
        v-model="formData.fdeptname"
        placeholder="请输入部门名称"
        clearable
        :style="{ width: '100%' }"
      ></el-input>
    </el-form-item>
    <el-form-item label="上级机构/部门">
      {{ formData.fparentname }}
    </el-form-item>
    <el-form-item label="部门编码">
      {{ formData.fdeptcode }}
    </el-form-item>
    <el-form-item label="顺序号">
      <el-input-number
        v-model="formData.forderindex"
        placeholder="请输入顺序号"
        :step="1"
        :min="0"
      ></el-input-number>
    </el-form-item>
    <el-form-item label="传真">
      <el-input
        v-model="formData.ffax"
        placeholder="请输入传真"
        clearable
        :style="{ width: '100%' }"
      ></el-input>
    </el-form-item>
    <el-form-item label="电话">
      <el-input
        v-model="formData.fphone"
        placeholder="请输入电话"
        clearable
        :style="{ width: '100%' }"
      ></el-input>
    </el-form-item>
    <el-form-item label="地址">
      <el-input
        type="textarea"
        v-model="formData.faddress"
        placeholder="请输入地址"
        :rows="2"
        :style="{ width: '100%' }"
      ></el-input>
    </el-form-item>
    <el-form-item size="large" style="    height: 60px;">
      <el-button type="primary" @click="submitForm">提交</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getDept, SaveDept } from '@/api/tappDept'
export default {
  components: {},
  props: ['datainfo'],
  data() {
    return {
      formData: {
        fdeptname: undefined,
        fparentname: undefined,
        ffax: undefined,
        forderindex: 0,
        fphone: undefined,
        faddress: undefined,
        fparentid: undefined,
        fid: undefined,
        fdeptcode: undefined
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    if (this.datainfo !== undefined && this.datainfo !== null) {
      if (this.datainfo.fid !== undefined && this.datainfo.fid !== null) {
        getDept(this.datainfo.fid).then(res => {
          if (res.code === 0) {
            this.formData.fdeptname = res.data.fdeptname
            this.formData.fparentname = this.datainfo.fparentname
            this.formData.ffax = res.data.ffax
            this.formData.forderindex = res.data.forderindex
            this.formData.fphone = res.data.fphone
            this.formData.faddress = res.data.faddress
            this.formData.fparentid = res.data.fparentid
            this.formData.fid = res.data.fid
            this.formData.fdeptcode = res.data.fdeptcode
          }
        })
      } else {
        Object.assign(this.formData, this.datainfo)
      }
    }
  },
  methods: {
    submitForm() {
      if (
        this.formData.fdeptname === '' ||
        this.formData.fdeptname === undefined
      ) {
        this.$message.success('请输入部门名称')
        return
      }
      if (
        this.formData.forderindex === '' ||
        this.formData.forderindex === undefined
      ) {
        this.$message.success('请输入顺序号')
        return
      }
      SaveDept(this.formData).then(res => {
        if (res.code === 0) {
          this.formData.fid = res.data.fid
          this.formData.fcode = res.data.fdeptcode
          this.$emit('savedata', this.formData)
          this.$message.success('保存成功！')
        }
      })
    },
    resetForm() {
      this.formData.fdeptname = undefined
      this.formData.ffax = undefined
      this.formData.forderindex = 0
      this.formData.fphone = 0
      this.formData.faddress = undefined
    }
  }
}
</script>
